/*
 * @Author: wcx 1055127249@qq.com
 * @Date: 2024-12-25 17:56:51
 * @LastEditors: wcx 1055127249@qq.com
 * @LastEditTime: 2025-01-17 17:28:41
 * @FilePath: \my-project\src\utils\request.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import axios from "axios"
import { getuserlist } from './mock'
import { generateUUIDv4, sleep } from "./index"

const http = {
  async get(url, params) {
    if (url === '/user/list' || url === '/api/user/list') {
      await sleep(500)
      await axios.get(url, { params })
      return {
        code: 200,
        total: 100,
        data: getuserlist(params),
      }
    } else {
      const res = await axios.get(url, { params })
      return {
        code: 200,
        data: res.data
      }
    }
  },
  async post(url, params) {
    await axios.post(url, params)
    await sleep(500)
    return {
      code: 200,
      data: generateUUIDv4()
    }
  },
}
export default http

export async function request(info, params = {}) {
  const { url, method, handler, before } = info
  if (typeof before === 'function') {
    params = before(params)
  }
  const res = http[method](url, params);
  if (typeof handler === 'function') {
    return handler(res)
  }
  return res
}

export async function getUserMenus(params) {
  const res = await axios.get('/user/menu.json')
  return res.data
}